/**
 * 
 */
package map;

import java.awt.FontMetrics;

import main.Controller;
import processing.core.PApplet;
import processing.core.PImage;

/**
 * @author Mathias
 *
 */
public class Station {
	private int id;
	private Location location;
	private String name;
	private PImage icon;
	public boolean isActive = false;

	private final double x;
	private final double y;
	
	private int time;

	public Station(int id, Location location, String name){
		this.id = id;
		this.location = location;
		this.name = name;
		x=location.getLongitude();
		y=location.getLatitude();
	}
	
	public void restore(){
		location.setLongitude(x);
		location.setLatitude(y);
	}


	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}



	/**
	 * @return the location
	 */
	public Location getLocation() {
		return location;
	}



	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}



	/**
	 * @return the time
	 */
	public int getTime() {
		return time;
	}

	/**
	 * @param time the time to set
	 */
	public void setTime(int time) {
		this.time = time;
	}

	public String toString(){
		return "Station (Name: "+this.getName()+" Id: "+this.getId()+" Location: "+this.location.getLatitude()+", "+this.getLocation().getLongitude()+")";
	}
	

	public void draw(PApplet applet, double zoomFactor){
		applet.ellipseMode(PApplet.CENTER);
		
		float x=Controller.roundTill((float) (this.location.getLongitude()*100),6);
		float iw= Controller.roundTill(Controller.roundTill(icon.width/Controller.getZoom()*0.5f,6)/2, 6);
		float y=Controller.roundTill(-(float)this.location.getLatitude()*100,6);
		float iy = Controller.roundTill(Controller.roundTill(icon.height/Controller.getZoom()*0.5f-10/Controller.getZoom(),6)/2, 6);
		float tw= Controller.roundTill(applet.textWidth(name)/2, 6);
		float twz=Controller.roundTill(tw, 6);
		
		applet.image(icon, Controller.roundTill(x-iw,6), -(float)this.location.getLatitude()*100-(icon.height/2), Controller.roundTill(Controller.roundTill(icon.width/Controller.getZoom(),3)*0.5f,3), Controller.roundTill(Controller.roundTill(icon.height/Controller.getZoom(),3)*0.5f, 3));
		applet.fill(0,0 ,0);
		applet.textSize(12/Controller.getZoom());
		applet.text(name, Controller.roundTill(x - twz, 6), -(float)this.location.getLatitude()*100-icon.height/2-(10/Controller.getZoom()));

	}
	
	public double getX(){
		return this.location.getLongitude();
	}
	
	public double getY(){
		return this.location.getLatitude();
	}
	
	public void setX(double x){
		this.location.setLongitude(x);
	}
	
	public void setY(double y){
		this.location.setLatitude(y);
	}
	
	public float getLongitudeScaled(){
		return (float) (this.location.getLongitude()*100-(icon.width/2));
	}
	
	public float getLatitudeScaled(){
		return -(float)this.location.getLatitude()*100-(icon.height/2);
	}
                                          
	public void loadIcon(PApplet applet){
		if (isActive){
			this.icon = applet.loadImage("resources/station_active.png");
		}
		else{
			this.icon = applet.loadImage("resources/station.png");
		}
		
	}


	public void setActive(boolean b, PApplet applet) {
		if (!isActive == b){
			isActive = b;
			this.loadIcon(applet);
		}
		
	}
}
